package de.gdgkarlsruhe.programminfo;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
 * 
 * @author GDG Karlsruhe
 */
public class TabActivity extends Activity implements TabListener {

  private static final String TAG = "TabActivity";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab);

    ActionBar actionBar = getActionBar();
    // Auf Tab-Navigation umstellen
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    // Tabs hinzuf�gen
    for (int i = 0; i < 3; i++) {
      actionBar
        .addTab(actionBar.newTab().setText("Track " + (i + 1)).setTabListener(this));
    }
  }
  

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {
    Log.d(TAG, "Tab selektiert: " + tab.getText());
//    TrackFragmentDemoData fragment = new TrackFragmentDemoData();
    TrackFragmentJsonData fragment = new TrackFragmentJsonData();
    fragment.init(tab.getPosition());
    ft.replace(R.id.content, fragment);
  }

  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) { }
  
  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) { }
  
  

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tab, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_settings:
      AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
      builder1.setMessage("Mein Alert");
      builder1.setCancelable(true);
      builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          dialog.cancel();
        }
      });
      builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          dialog.cancel();
        }
      });

      builder1.show();
      break;
    }
    return super.onOptionsItemSelected(item);
  }

}
